﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Com.Framework;
using Com.Framework.Data;
using Com.Framework.Cache;
using Com.Library.DB.UserCard;
using Com.Library.SOAP.PointHistory;
using Com.Web.coffinegurunaru.AppLogic.Page;
using Com.Web.coffinegurunaru.net.pncoffice.www;

namespace Com.Web.coffinegurunaru.mypage
{
	public partial class card_info : WebPage
	{
		public List<UserCardEntity> CardList
		{
			get;
			set;
		}

		public List<PointHistoryEntity> PointList
		{
			get;
			set;
		}

		public string CardNo
		{
			get;
			set;
		}

		public int PageNo
		{
			get;
			set;
		}

		public byte PageSize
		{
			get { return 10; }
		}

		public string FromDate
		{
			get;
			set;
		}

		public string ToDate
		{
			get;
			set;
		}

		public int TotalPoint
		{
			get;
			set;
		}

		public int UsedPoint
		{
			get;
			set;
		}

		public int NowPoint
		{
			get;
			set;
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			this.LeftMenuType = 1;
			
			PageNo = Request.QueryString["PageNo"].Parse(1);
			if (PageNo < 1)
				PageNo = 1;
			CardNo = Request.QueryString["CardNo"].Parse(string.Empty);
			FromDate = Request.QueryString["FromDate"].Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd"));
			ToDate = Request.QueryString["ToDate"].Parse(DateTime.Now.ToString("yyyy-MM-dd"));
			this.TotalPoint = 0;
			this.UsedPoint = 0;
			this.NowPoint = 0;

			ListData<UserCardEntity, OutputEntity> result = UserCardModel.GetList(this.WebCookies.UserNo, CodeCardStatus.Create, 1, 100);
			CardList = result.Record;
			int TotalRowCount = 0;

			if ( CardList.Count > 0 )
			{
				if (CardNo == string.Empty)
				{
					CardNo = CardList[0].CardNo;
				}
				string PointHistoryName = "_HISTORY_" + this.WebCookies.UserNo.ToString() + "_" + FromDate.Replace("-", "_") + ToDate.Replace("-", "_");
				string UserInfoName = "_USERINFO_" + this.WebCookies.UserNo.ToString();
				PncCrmService CRM = null;
				DataSet ds = CacheUtil.Get(PointHistoryName) as DataSet;
				if (ds == null)
				{
					CRM = new PncCrmService();
					ds = CRM.getMemberPointHistory(1640, CardNo, 0, FromDate, ToDate);
					CacheUtil.Insert(PointHistoryName, ds, 3);
				}

				string JoinText = string.Empty;
				int SalePoint = 0;
				int UsedPoint = 0;
				int RemPoint = 0;

				UserPointInfo PointInfo = CacheUtil.Get(UserInfoName) as UserPointInfo;
				if (PointInfo == null)
				{
					if (CRM == null)
						CRM = new PncCrmService();
					CRM.getMemberPointInfo(1640, CardNo, ref JoinText, ref SalePoint, ref UsedPoint, ref RemPoint);
					TotalPoint = SalePoint;
					this.UsedPoint = UsedPoint;
					NowPoint = RemPoint;
					UserPointInfo info = new UserPointInfo();
					info.TotalPoint = SalePoint;
					info.UsedPoint = UsedPoint;
					info.NowPoint = RemPoint;
					CacheUtil.Insert(UserInfoName, info, 10);
				}
				else
				{
					TotalPoint = PointInfo.TotalPoint;
					this.UsedPoint = PointInfo.UsedPoint;
					NowPoint = PointInfo.NowPoint;
				}

				PointList = PointHistoryModel.GetList(ds, PageNo, PageSize, out TotalRowCount);
			}
			else
			{
				UC_Paging.Visible = false;
				PointList = new List<PointHistoryEntity>();
			}

			UC_Paging.TotalCount = TotalRowCount;
			UC_Paging.PageName = "PageNo";
			UC_Paging.PageNo = PageNo;
			UC_Paging.PageSize = PageSize;
			UC_Paging.Query = "CardNo=" + CardNo + "&FromDate=" + FromDate + "&ToDate=" + ToDate;
		}
	}

	public class UserPointInfo
	{
		public UserPointInfo()
		{
		}

		public int TotalPoint
		{
			get;
			set;
		}
		public int UsedPoint
		{
			get;
			set;
		}
		public int NowPoint
		{
			get;
			set;
		}
	}
}
